<?php
/**
 * User: Jerry
 * Date: 2017/7/24
 * Time: 17:04
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class HomeTemplateVogueModel
 * @author Jerry
 * @package SDXApi\Model
 */
class HomeTemplateVogueModel extends Model
{
    /**
     * @var string
     */
    protected $tableName = 'home_template_vogue';


    /**
     * @author Jerry
     * @param int $templateId
     * @return mixed
     */
    public function findVogue($templateId = 0)
    {
        $where = array(
            'template_id'   =>  $templateId,
        );

        return $this->where($where)->select();
    }

    /**
     * @author Jerry
     * @param string $ids
     * @param int $limit
     * @param int $wid
     * @return mixed
     */
    public function findVogueGoodsId($ids = '', $limit = 0, $wid = 8)
    {
        $time = gmtime();
        $where = array(
            'promote_start_date'    =>  array('LT', $time),
            'promote_end_date'      =>  array('GT', $time),
            'promote_image'         =>  array('NEQ', ''),
            'is_delete'             =>  0,
            'is_on_sale'            =>  1,
            'is_buynow'             =>  array('NEQ', 1),
        );

        if (! empty($ids)) {
            $where['cat_id']    =  array('IN', $ids);
        }

        $data = $this->table('sdx_goods_wid_' . $wid)->field('goods_id')->where($where)->limit($limit)->select();

        return $data;
    }

    /**
     * @author Jerry
     * @param string $ids
     * @param int $limit
     * @param int $wid
     * @return mixed
     */
    public function findVogueGoodsInCategoryId($ids = '', $limit = 0, $wid = 8)
    {
        $time = gmtime();

        $where = array(
            'g.cat_id'      =>  array('IN', $ids),
            'wg.is_delete'	=>	0,
            'wg.is_on_sale'	=>	1,
            'g.promote_image'   =>  array('NEQ', ''),
            'g.promote_start_date'    =>  array('LT', $time),
            'g.promote_end_date'      =>  array('GT', $time),
        );

        $field = 'g.goods_id,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.delivery_cycle,gs.practical_num,g.promote_user_rank,g.promote_image';

        $join = "LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id AND gs.wid = $wid";
        $wJoin = "LEFT JOIN sdx_goods_wid_$wid AS wg ON g.goods_id = wg.goods_id";

        return $this
            ->table('sdx_goods')
            ->alias('g')
            ->field($field)
            ->join($join)
            ->join($wJoin)
            ->where($where)
            ->order('gs.practical_num desc')
            ->limit($limit)
            ->group('g.goods_id')
            ->select();
    }

    /**
     * @author Jerry
     * @param string $ids
     * @param int $wid
     * @param int $limit
     * @return mixed
     */
    public function getVogueGoodsByIds($ids = '', $wid = 8, $limit = 9)
    {
        $where = array(
            'g.goods_id'  =>  array('IN', $ids),
            'g.is_delete'	=>	0,
            'g.is_on_sale'	=>	1,
            'g.promote_image'   =>  array('NEQ', ''),
        );

        $field = 'g.goods_id,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.delivery_cycle,gs.practical_num,g.promote_user_rank,g.promote_image';

        $join = "LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id AND gs.wid = $wid";
        return $this
            ->table('sdx_goods')
            ->alias('g')
            ->field($field)
            ->join($join)
            ->where($where)
            ->order('gs.practical_num desc')
            ->limit($limit)
            ->select();
    }
}